﻿#include "secinfo.h"
#include<QTextStream>
#include<QJsonDocument>
#include<QJsonObject>
#include<QJsonParseError>
#include<QDebug>

SecInfo::SecInfo() :gas(0), hum(0), Illegal(0), tem(0), subNum(1)/*默认是一号结点发送*/, mac("")
{


}

//SecInfo::SecInfo(const SecInfo &secinfo)
//{
//    this->gas = secinfo.getGas();
//    this->hum = secinfo.getHum();
//    this->tem = secinfo .getTem();
//    this->Illegal = secinfo.getIllegal();

//}

// 右值构造函数
SecInfo::SecInfo(SecInfo &&secinfo)
{
	qDebug() << "right value constructor";

	this->gas = secinfo.getGas();
	this->hum = secinfo.getHum();
	this->tem = secinfo.getTem();
	this->Illegal = secinfo.getIllegal();

	// 设置 子结点的编号
	this->subNum = secinfo.getSubNum();
	this->mac = secinfo.getMac();
}

int SecInfo::getGas() const
{
	return this->gas;

}

int SecInfo::getTem() const
{
	return this->tem;
}

int SecInfo::getIllegal() const
{
	return this->Illegal;
}

int SecInfo::getHum() const
{
	return this->hum;
}

QString SecInfo::getMac() const
{
	return this->mac;
}

int SecInfo::getSubNum() const
{
	return this->subNum;
}

QString SecInfo::getSubMac() const
{
	return this->subMac;
}

void SecInfo::setGas(int gas)
{
	this->gas = gas;
}

void SecInfo::setTem(int tem)
{
	this->tem = tem;
}

void SecInfo::setIllegal(bool i)
{
	this->Illegal = i;
}

void SecInfo::setHum(int hum)
{
	this->hum = hum;
}

void SecInfo::setMac(QString macaddr)
{
	this->mac = macaddr;
}

void SecInfo::setSubNum(int subNum)
{
	this->subNum = subNum;
}

void SecInfo::setSubMac(const QString &mac)
{
	this->subMac = mac;
}



QString SecInfo::toJSON()
{
	QJsonObject json;
	json.insert("tem", this->getTem());
	json.insert("hum", this->getHum());
	json.insert("gas", this->getGas());
	json.insert("illegal", this->getIllegal());
	json.insert("subNum", this->getSubNum());
	json.insert("mac", this->getMac());
	json.insert("subMac", this->getSubMac());
	// 构建 JSON 文档
	QJsonDocument document;
	document.setObject(json);
	QByteArray byteArray = document.toJson(QJsonDocument::Compact);
	QString strJson(byteArray);



	return strJson;

}

//  将json 字符串反序列化成一个 对象 然后输出
SecInfo SecInfo::parseSecInfo(QString text)
{
	SecInfo secinfo;
	//
	QJsonDocument jsonDocument = QJsonDocument::fromJson(text.toLocal8Bit().data());
	if (jsonDocument.isNull()) {
		qDebug() << "String NULL" << text.toLocal8Bit().data();
		return secinfo;
	}
	QJsonObject jsonObject = jsonDocument.object();

	auto tem = jsonObject.value("tem");
	auto hum = jsonObject.value("hum");
	auto illegal = jsonObject.value("illegal");
	auto gas = jsonObject.value("gas");
	auto sub = jsonObject.value("subNum");
	auto macaddr = jsonObject.value("mac");


	secinfo.setTem(tem.toInt());
	secinfo.setHum(hum.toInt());
	secinfo.setIllegal(illegal.toInt());
	secinfo.setGas(gas.toInt());


	secinfo.setSubNum(sub.toInt());
	secinfo.setMac(macaddr.toString());

	secinfo.setSubMac(jsonObject.value("subMac").toString());


	return secinfo;

}